const fs = require('fs-extra');
const { parse } = require('url');
async function transformRequest(url, server) {
  const { pluginContainer } = server;
  const { id } = await pluginContainer.resolveId(url); //获取此文件的绝对路径
  const loadResult = await pluginContainer.load(id); // 加载此文件的内容
  let code;
  if (loadResult) {
    code = loadResult.code;
  } else {
    let fsPath = parse(id).pathname;
    code = await fs.readFile(fsPath, 'utf-8'); // 没有 loadResult 则通过 fs 读取文件内容
  }
  await server.moduleGraph.ensureEntryFromUrl(url);
  //转换文件内容( main.js 的内容时会通过 transform 将 'vue' 修改为 'xxx/node_modules/.vite/deps/vue.js' )
  const transformResult = await pluginContainer.transform(code, id);
  return transformResult;
}
module.exports = transformRequest;
